package com.jintoufs.service.user.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.user.UserDAO;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.user.User;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.user.PasswordHelper;
import com.jintoufs.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDAO userDAO;
    @Autowired
    private PasswordHelper passwordHelper;

    @Override
    @SystemServiceLog(description = "获取用户列表")
    public List<Map> getByFilter(QueryFilters filters) {
        return userDAO.getByFilter(filters);
    }

    @Override
    @SystemServiceLog(description = "新增用户")
    public User add(User user) {
        passwordHelper.encryptPassword(user);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", user.getUserName());
        List<User> list = userDAO.queryByParams(params);
        if (list == null || list.size() == 0) {
            userDAO.insert(user);
            return user;
        } else {
            return null;
        }
    }

    @Override
    @SystemServiceLog(description = "修改用户信息")
    public void update(User user) {
        userDAO.update(user);
    }

    @Override
    @SystemServiceLog(description = "删除用户")
    public void delete(Integer id) {
        userDAO.delete(id);
    }

    @Override
    @SystemServiceLog(description = "用户登录")
    public User login(String trim, String md5Encoder) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", trim);
        params.put("password", md5Encoder);
        List<User> list = userDAO.queryByParams(params);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<User> queryByParams(Map<String, Object> params) {
        return userDAO.queryByParams(params);
    }

    @Override
    public List<User> getUserByRoleName(String roleName) {
        return userDAO.getUserByRoleName(roleName);
    }

    @Override
    public Map<String, Object> queryUserById(Integer id) {
        return userDAO.queryUserById(id);
    }

    @Override
    public User queryByPrimaryKey(Integer id) {
        return userDAO.queryByPrimaryKey(id);
    }

    @Override
    public String getUserRoleNameById(Integer id) {
        return userDAO.getUserRoleNameById(id);
    }

    @Override
    public List<String> getFingerList() {
        return userDAO.getFingerList();
    }

    @Override
    public List<String> getCodeList() {
        return userDAO.getCodeList();
    }

    @Override
    public List<User> queryUsersByTaskId(Map<String, Object> params) {
        return userDAO.queryUsersByTaskId(params);
    }

    @Override
    @SystemServiceLog(description = "初始化密码")
    public User initPassword(Integer id) {
        User user = userDAO.queryByPrimaryKey(id);
        user.setPassword("666666");
        user.setLastLoginTimeSetNull((byte) 1);
        user.setModified(Constants.USER_PASSWORD_NOT_MODIFY);
        passwordHelper.encryptPassword(user);
        userDAO.update(user);
        return user;
    }

}
